package com.reger.l2cache.bloomfilter.hash;

import com.reger.l2cache.bloomfilter.Hash;

/**
 * 哈希函数类
 * 
 * @author leige
 *
 */
public class SimpleHash implements Hash {
	private long cap;
	private int seed;

	public SimpleHash(long cap, int seed) {
		this.cap = cap;
		this.seed = seed;
	}

	/**
	 * hash函数，采用简单的加权和hash
	 * 
	 * @param value string
	 * @return hashcode
	 */
	@Override
	public long hash(String value) {
		int result = 0;
		int len = value.length();
		for (int i = 0; i < len; i++) {
			result = seed * result + value.charAt(i);
		}
		return (cap - 1) & result;
	}
}